package com.ssyt.yt1.day16;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * LinkedList：增删效率高、查询效率低 <br/>
 * ArrayList：反过来！
 * 
 * @author Administrator
 *
 */
public class LinkedListDemo {

	/**
	 * list.add(new Object());反而ArrayList更快！
	 * 
	 * @param list
	 */
	public static void test1(List<Object> list) {
		long start = System.currentTimeMillis();

		for (int i = 0; i < 50000; i++) {
			list.add(0, new Object());
		}

		if (list instanceof ArrayList) {
			System.out.println("ArrayList:"
					+ (System.currentTimeMillis() - start));
		}
		if (list instanceof LinkedList) {
			System.out.println("LinkedList:"
					+ (System.currentTimeMillis() - start));
		}
	}

	/**
	 * 添加到头部或尾部
	 */
	public static void test2() {
		LinkedList<Object> list = new LinkedList<Object>();
		list.add("world");
		list.addFirst("hello");
		System.out.println(list);
	}

	public static void main(String[] args) {
		// test1(new ArrayList<Object>());
		// test1(new LinkedList<Object>());
		test2();
	}
}
